Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
How to handle "post" request in node.js in a one-page web app

I know there are a few answers matching this question already. But I am looking for something else.

In the client side I have a signUp form that will send a post request to the server with username and password.

In the server side I authenticate the request. This is the code. (I use express)

server.post('/', function(req, res) {

 res.redirect('/');// I want to get rid of the "confirm resubmission message upon refreshing the page"
 authenticate(req.body.username){
        if(false){
           client.emit('signUpResponse',{success:false}); //cannot do it without a client's socket id
        } else {
        db.user.insert({username:req.body.username})
            client.emit('signUpResponse',{success:true}); //cannot do it without a client's socket id
        }
    };

At this stage I don't want to make another webpage(like/signup) to handle the signUp event.

I can access the client's socket id if I put the above code under the io.sockets.on code.

io.sockets.on('connection', function (client) {  

 server.post('/', function(req, res) {
 console.log(client.id) //now I can access the client's socket
 res.redirect('/');// I want to get rid of the "confirm resubmission message upon refreshing the page"
 authenticate(req.body.username){
        if(false){
           client.emit('signUpResponse',{success:false}); //nothing happens at the client' side. maybe something to do with the "res.redirect"?
        } else {
        db.user.insert({username:req.body.username})
            client.emit('signUpResponse',{success:true}); ////nothing happens at the client' side. maybe something to do with the "res.redirect"?
        }
    };

In the server.post handler, I dont want to use any method like res.send,res.redirect(newURL) as I want the client side to stay at the same page. So is there a way to communicate with client side in the "post" handler like client emit?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I take it that the site is using https. With that said you should just use socket.io. It's much more easier.

// On the client-side
var client = io();

client.on('userLoggedIn', function(confirm) {
  if (confirm) {
    // Do your post login confirmation stuff here
  }
});

var form = document.getElementById('login-form'),
  username = document.getElementById('username').value,
  password = document.getElementById('password').value;

form.addEventListener('submit', function(event) {
  client.emit('login', {
    username: username,
    password: password
  });

  // Stops the form from redirecting the web page to the action link
  event.preventDefault();
}, false);

// Server-side
io.on('connection', function(client) {
  // Login user
  client.on('login', function(user) {
    // Do your login stuff here
  });
});

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda